package org.zjt.scala.book

import scala.collection.mutable

/**
  *
  * Description:
  *
  *     映射 ： Map （不可变）、mutable.HashMap (可变的) 、取值[（）、get]、更新、迭代、排序
  *     元素： tuple 、tuple_index
  *     zip:
  *
  * @author juntao.zhang
  *         Date: 2018-06-27 下午6:39
  * @see
  */
class Chapter4 {

}

object Chapter4 extends App {

  /** 声明一个不可变 immutable map**/
  var map = Map("key1" -> "value1","key2" -> "value2")

  // error  map 不能改变元素、长度
  // map("key1") = "ssss"

  println(s"get key1 :  ${map("key1")}")
  println(s"get key1 :  ${map.getOrElse("key1","default value")}")

  /**  MAP get Option   **/
  println(s"get key1 :  " +
    s"${map.get("key12")
    match {
      case Some(result) => result
      case None => None
    }}")


  var mutableMap = mutable.HashMap("key1" -> "value1","key2" -> "value2")

  mutableMap("key1") = "new-value1"
  mutableMap("key3") = "value3"
  mutableMap += ("key5" -> "value5","key4" -> "value4")

  for ((k,v) <- mutableMap){
    println(k,v)
  }

  var map1 = Map(("wewew","sssss"),("ssss","sssz"))
  for ((k,v) <- map1){ println(k,v) }


  var map2 = mutableMap + ("wsd" -> "sdsd")
  var sortedMap = mutable.SortedMap.apply(("wewew","sssss"),("ssss","sssz"))
  for ((k,v) <- sortedMap){ println(k,v) }


  var tuple1 = Tuple3(1,"zhang",23)
  println(tuple1._1,tuple1 _2,tuple1 _3)

  var (f,s,t) = tuple1
  println(f,s,t)


  /**元祖 **/
  var array = Array("2","3","4")
  var pairs = array.zipWithIndex
  for((k,v) <- pairs) { println(k,v) }

  var array2 = for(i <- 0 until(5,2) ) yield i
  var pairs2 = array2.zip(array)
  for((k,v) <- pairs2) { println(k,v) }


  println("hello".zip("world"))

}
